package com.qipay.mapper;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * Creator: Yao
 * Date:    2017/10/13
 * For:
 * Other:
 */
public class OperateLogSqlProvider extends com.qipay.baas.mapper.OperateLogSqlProvider {

    public String selectByUidAndType(Map<String, Object> params) {
        if (params != null && params.size() > 2) {
            String sql = "SELECT * FROM `operate_log` where ";
            String orderby = " ORDER BY id desc LIMIT #{offset},#{size}";
            if (params.getOrDefault("url", null) != null) {
                sql += "`url` like concat('/',#{url},'%') and";
            }
            if (params.getOrDefault("uid", null) != null) {
                sql += "`user_id`=#{uid} and";
            }
            return StringUtils.stripEnd(sql, "and") + orderby;
        }
        return "SELECT * FROM `operate_log` ORDER BY id desc LIMIT #{offset},#{size}";

    }

    public String countByUidAndType(Map<String, Object> params) {
        if (params != null && params.size() > 0) {
            String sql = "SELECT count(0) FROM `operate_log` where ";
            if (params.getOrDefault("url", null) != null) {
                sql += "`url` like concat('/',#{url},'%') and";
            }
            if (params.getOrDefault("uid", null) != null) {
                sql += "`user_id`=#{uid} and";
            }
            return StringUtils.stripEnd(sql, "and");
        }
        return "SELECT count(0) FROM `operate_log` limit 1";

    }
}
